class GroupController < ApplicationController
	#layout 'standard'
	before_filter :login_required, :except => ['notauthorized', 'result', ]
	
	
	def manage_acl
		@groupId = params[:group][:id] if !params[:group].nil?
		@groupId = params[:id]  if @groupId.nil?
		#@groupId = 1 if @groupId.nil?
		
		@hKontrols = getHashOfControllers
		@hKontrols = Groupacl.getGroupAcl(@hKontrols, @groupId)

		@groups = Group.find(:all)
		@group = Group.find_by_id( @groupId)

		dbSync, errMsg = compareCtlToDb(@hKontrols)
		if  dbSync == false
			flash[:dbnotsycn] ="Your Database Not Sync!!<br>" + errMsg.to_s
		end
		#semakKontrol(@hKontrols)
	end
	def manage_member
		@groupId = params[:group][:id] if !params[:group].nil?
		@groupId = params[:id]  if @groupId.nil?
		#@groupId = 1 if @groupId.nil?

		if @groupId.to_s.length > 0
			@group = Group.find_by_id( @groupId)
			#@groupName = @grp.name
		end
		@groups = Group.find(:all)
		@users = User.find(:all, :select => " id, login, active ")
		@ugroups = Usergroup.find(:all, :conditions => ["group_id = ? ", @groupId])
		@hashMember = Usergroup.getGroupMembers(@users, @ugroups)
		puts "<manage_member> @groupId=#{@groupId}"
	end
	
	def update_member
		@groupId = params[:group][:id] if !params[:group].nil?
		if @groupId.nil?
			redirect_to :action => "manage_member" 
			return
		end
		
		newMember = params[:member]
		Usergroup.updateDb(@groupId, newMember)
		
		flash[:message] = "Group Members Update"
		redirect_to :action => "manage_member", :id => @groupId
	end
	
	def updateacl
		@groupId = params[:group][:id] if !params[:group].nil?
		if @groupId.nil?
			redirect_to :action => "manage_acl" 
			return
		end
		
		newAcl = params[:acl]
		@hKontrols = getHashOfControllers
		@hKontrols = Groupacl.getGroupAcl(@hKontrols, @groupId)
		group = Group.find(@groupId)
		group.update_attributes(params[:group])
		
		Groupacl.updateDb(@groupId,@hKontrols, newAcl)
		flash[:message] = "Group Members Previleges Updated"
		redirect_to :action => "manage_acl", :id => @groupId
	end
	
	def user_show
		@user = User.find(params[:id])
		@ugroups = Usergroup.find_by_sql("select name from groups where id in(select group_id from usergroups where  user_id = " + @user.id.to_s + ") ")
	end
	def user_edit
		@user = User.find(params[:id])
		@user.email_confirmation = @user.email
		
		hashPass_ori = @user.hashed_password
		puts "<update> #{hashPass_ori}  #{@user.login} #{@user.email}"
		if request.post?
			@user.update_attributes(params[:user])
			@user.password = "dummy"  # need this one because of validates_presence_of in User model
			@user.password_confirmation = "dummy"
			@user.hashed_password = hashPass_ori 
			if @user.save()
				flash[:message] = "User information update."
				redirect_to :action => "user_show", :id => @user.id
			else
				flash[:warning] = "Update unsuccessful"
				puts "<update>2 #{@user.hashed_password}    #{@user.login} #{@user.email}"
			end
		end
	end
	def user_password
		@user=User.find(params[:id])
		if request.post?
			@user.update_attributes(:password => params[:user][:password], 
									:password_confirmation => params[:user][:password_confirmation])
			if @user.save
				flash[:message]="Password Changed"
			end
		end
	end
	
	
	
	def group_edit
		@groupId = params[:id] 		
		@group = Group.find(@groupId)
		if request.post?
			@group.update_attributes(params[:group])
			if @group.save()
				flash[:message] = "Group information update."
				redirect_to :action => "manage_acl", :id => @group.id
			else
				flash[:warning] = "Update unsuccessful"
				#puts "<update>2 #{@user.hashed_password}    #{@user.login} #{@user.email}"
			end
			
		end
	end
	def group_new
		@group = Group.new()
		puts "<group_new> group!=nil "if !@group.nil?
		puts "<group_new> group.id=nil "if @group.id.nil?
		p @group
		render :action => "group_edit"
	end
	def group_create
		if request.post?
			@group = Group.new()
			@group.update_attributes(params[:group])
			if @group.save()
				flash[:message] = "Group information update."
				redirect_to :action => "manage_acl", :id => @group.id
			else
				flash[:warning] = "Update unsuccessful"
			end
		end
	end
	def group_delete
		@groupId = params[:id]
		group = Group.find(@groupId)
		if group.destroy
			flash[:message] = "Group Deleted."
			redirect_to :action => "manage_acl"
		end
	end
	
		
	public
	def paddingAclTable
		hKontrol = getHashOfControllers
		Groupacl.paddingAclTable(hKontrol, true)	
		flash[:message] = "Finish Updating ACL in Database"
		redirect_to :action => "manage_acl"
	end
	
	def hasil
		kontroller ="mohon"
		kontroller = kontroller.camelize() + "Controller"
		puts "+"*20, kontroller
		
		puts kontrol ="mohon"
		puts [:params]
	end
	def notauthorized
	
	end
	
	private 
	def getHashOfControllers
		kontrol = Hash.new()

		controllers = Dir.new("#{RAILS_ROOT}/app/controllers").entries
		controllers.each do |controller|
			if controller =~ /_controller\.rb$/ 
				cont = controller.camelize.gsub(".rb","")
				hsAktion = Hash.new()
				#puts "----------------#{cont}"
				'''  old method to list action of controller
				arAktion = (eval("#{cont}.new.methods")) - ApplicationController.methods - Object.methods - ApplicationController.new.methods
				arAktion.each{|akt|
					hsAktion[akt] = false
				}
				'''

				acts = eval("#{cont}.action_methods")
				acts.each{|act|
					hsAktion[act] = false
				}
				
				kontrol[cont] = hsAktion
				#print cont, "--",  kontrol.class,"--",kontrol[cont].class,"--\n"
			end
		end
		
		kontrol.delete("ApplicationController")
		kontrol.delete("UserController")
		#kontrol.delete("GroupController")
		return kontrol
	end
	def getHashOfControllers_new
		#but this one need to restart rails for new controller
		ActionController::Routing.possible_controllers.each{|ctl|
			acts = eval("#{ctl}.action_methods")
			acts.each{||
			}
		}
	end
	
	def compareCtlToDb(hCtl)
		Groupaction.compareCtlToDb(hCtl)
	end
	
	def semakKontrol(kontrol)
		print "<semakKontrol>  Begin",kontrol.class , "\n"
		kontrol.each{|ctl, hact|
			hact.each{|act,val|
				print "<semakKontrol> ctl=#{ctl}  act=#{act}   permission=  #{val} \n"
			}
		}
	end
	
end

